<?php
/**
 * Created by PhpStorm.
 * User: worry
 * Date: 2019/1/5
 * Time: 11:30
 */

namespace frontend\controllers;

use common\components\Error;
use common\controllers\BaseController;
use common\services\InformationService;
use common\services\UploadService;
use common\helps\Func;
use common\models\Information;
use Yii;
use yii\web\Response;

class IndexController extends BaseController
{
    public function actionIndex()
    {
        $sale_id = Yii::$app->request->get('id');
        if (Yii::$app->request->isPost) {
            $res = Error::tryCatch(function () {
                $db = Yii::$app->db;
                \Yii::$app->response->format = Response::FORMAT_JSON;
                // 表单提交保存数据
                $post = Yii::$app->request->post();
                $service = new InformationService();
                $res = $service->save(['Information' => $post]);
                return $this->success('提交申请成功');
            });
            return $res;
        }
        return $this->render('index', ['sale_id' => $sale_id]);
    }

    public function actionTest()
    {
        echo Func::password('123456');
        die;
    }

    /**
     * 上传文件
     */
    public function actionUpload()
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $res = Error::tryCatch(function () {
            $service = new UploadService();
            $res = $service->save([]);
            if ($res !== false) {
                return $this->success('上传文件成功', ['path' => $res]);
            } else {
                throw new \Exception('上传文件失败');
            }
        });
        return $res;
    }
}